// @algorithm @lc id=517 lang=cpp 
// @title super-washing-machines


// @test([1,0,5])=3
// @test([0,3,0])=2
// @test([0,2,0])=-1
class Solution {
public:
    int findMinMoves(vector<int>& machines) {
        int totalCloth = accumulate(machines.begin(), machines.end(), 0);
        if(totalCloth % machines.size()) return -1;
        int avgCloth = totalCloth / machines.size();
        int sumDiff = 0, ans = 0;
        for(auto cloth : machines){
            cloth -= avgCloth;
            sumDiff += cloth;
            int step = max(abs(sumDiff), cloth);
            if(step > ans) ans = step;
        }
        return ans;
    }
};